package com.xfc.web.controller.school;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xfc.common.core.domain.entity.SysUser;
import com.xfc.school.domain.ScCourseList;
import com.xfc.school.domain.ScItemChangeRecord;
import com.xfc.school.domain.ScStudent;
import com.xfc.school.service.IScItemChangeRecordService;
import com.xfc.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xfc.common.annotation.Log;
import com.xfc.common.core.controller.BaseController;
import com.xfc.common.core.domain.AjaxResult;
import com.xfc.common.enums.BusinessType;
import com.xfc.school.domain.ScItemList;
import com.xfc.school.service.IScItemListService;
import com.xfc.common.utils.poi.ExcelUtil;
import com.xfc.common.core.page.TableDataInfo;

/**
 * 物品Controller
 * 
 * @author xfc
 * @date 2021-12-22
 */
@RestController
@RequestMapping("/school/goods")
public class ScItemListController extends BaseController
{
    @Autowired
    private IScItemListService scItemListService;

    @Autowired
    private IScItemChangeRecordService scItemChangeRecordService;

    @Autowired
    private ISysUserService userService;
    /**
     * 查询物品列表
     */
    @PreAuthorize("@ss.hasPermi('school:goods:list')")
    @GetMapping("/list")
    public TableDataInfo list(ScItemList scItemList)
    {
        startPage();
        List<ScItemList> list = scItemListService.list(Wrappers.query(scItemList).orderByDesc("update_time"));
        return getDataTable(list);
    }

    /**
     * 导出物品列表
     */
    @PreAuthorize("@ss.hasPermi('school:goods:export')")
    @Log(title = "物品", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ScItemList scItemList)
    {
        List<ScItemList> list = scItemListService.list();
        ExcelUtil<ScItemList> util = new ExcelUtil<ScItemList>(ScItemList.class);
        return util.exportExcel(list, "list");
    }

    /**
     * 获取物品详细信息
     */
    @PreAuthorize("@ss.hasPermi('school:goods:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(scItemListService.getById(id));
    }

    /**
     * 新增物品
     */
    @PreAuthorize("@ss.hasPermi('school:goods:add')")
    @Log(title = "物品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ScItemList scItemList)
    {

        scItemList.setCreateTime(new Date());
        scItemList.setUpdateTime(new Date());
        return toAjax(scItemListService.save(scItemList));
    }

    /**
     * 修改物品
     */
    @PreAuthorize("@ss.hasPermi('school:goods:edit')")
    @Log(title = "物品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScItemList scItemList, @RequestBody ScItemChangeRecord scItemChangeRecord,Authentication authentication)
    {
        //插入物品变更记录-S
        //获取登录用户信息
        SysUser user=userService.selectUserByUserName(authentication.getName());
        scItemChangeRecord.setOperator(user.getNickName()+"("+user.getUserName()+")");


        StringBuilder opertionStr=new StringBuilder();//操作描述

        QueryWrapper<ScItemList> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",scItemList.getId());
        ScItemList item = scItemListService.getOne(queryWrapper);

        if (!item.getGoodsName().equals(scItemList.getGoodsName())){
            opertionStr.append("名称修改为："+scItemList.getGoodsName());
        }
        if (!item.getGoodsNumber().equals(scItemList.getGoodsNumber())){
            if ( !"".equals(opertionStr.toString())){
                opertionStr.append("<br>");
            }
            if(item.getGoodsNumber()>scItemList.getGoodsNumber()){
                opertionStr.append("出售数量：："+(item.getGoodsNumber()-scItemList.getGoodsNumber()));
            }else{
                opertionStr.append("采购数量："+(scItemList.getGoodsNumber()-item.getGoodsNumber()));
            }

        }
        if (item.getGoodsOriginalPrice().compareTo(scItemList.getGoodsOriginalPrice())!=0){
            if ( !"".equals(opertionStr.toString())){
                opertionStr.append("<br>");
            }
            opertionStr.append("原价修改为："+scItemList.getGoodsOriginalPrice());
        }

        if (item.getGoodsPrice().compareTo(scItemList.getGoodsPrice())!=0){
            if ( !"".equals(opertionStr.toString())){
                opertionStr.append("<br>");
            }
            opertionStr.append("售价修改为："+scItemList.getGoodsPrice());
        }

        if (!item.getGoodsState().equals(scItemList.getGoodsState())){
            if ( !"".equals(opertionStr.toString())){
                opertionStr.append("<br>");
            }
            if("1".equals(scItemList.getGoodsState())){
                opertionStr.append("状态修改为:停用");
            }else if("0".equals(scItemList.getGoodsState())){
                opertionStr.append("状态修改为:启用");
            }else{
                opertionStr.append("状态修改异常");
            }

        }

        scItemChangeRecord.setPedagogicalOperation(opertionStr.toString());
        scItemChangeRecord.setCreateTime(new Date());
        scItemChangeRecord.setUpdateTime(new Date());
        scItemChangeRecordService.save(scItemChangeRecord);
        //插入物品变更记录-E

        scItemList.setUpdateTime(new Date());
        return toAjax(scItemListService.updateById(scItemList));
    }

    /**
     * 删除物品
     */
    @PreAuthorize("@ss.hasPermi('school:goods:remove')")
    @Log(title = "物品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids){
		List<String> list=new ArrayList<>();
    	if (ids!=null&&ids.length>0) {
            for (String id : ids) {
                list.add(String.valueOf(id));
            }
		}
        return toAjax(scItemListService.removeByIds(list));
    }
}
